<?php
namespace BP\Sys;

use BP\En\EntityMyPK;

require_once("BP/En/Map.php");
require_once("BP/Tools/FtpUtil.php");
class FrmAttachmentDB extends EntityMyPK {
	public function getSort () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$Sort);
	}
	public function setSort ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$Sort, $value);
	}
	public function getRDT () 
	{
		$str = $this->GetValStringByKey(FrmAttachmentDBAttr::$RDT);
		return $str->substring(5, 16);
	}
	public function setRDT ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$RDT, $value);
	}
	public function getFileFullName () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$FileFullName);
	}
	public function setFileFullName ($value) // [String value]
	{
		$str = $value;
		$str = $str->replace("~", "-");
		$str = $str->replace("'", "-");
		$str = $str->replace("*", "-");
		$this->SetValByKey(FrmAttachmentDBAttr::$FileFullName, $str);
	}
	public function getUploadGUID () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$UploadGUID);
	}
	public function setUploadGUID ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$UploadGUID, $value);
	}
	public function getFilePathName () 
	{
		return $this->getFileFullName()->substring(($this->getFileFullName()->lastIndexOf('\\') + 1));
	}
	public function getFileName () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$FileName);
	}
	public function setFileName ($value) // [String value]
	{
		$str = $value;
		$str = $str->replace("~", "-");
		$str = $str->replace("'", "-");
		$str = $str->replace("*", "-");
		$this->SetValByKey(FrmAttachmentDBAttr::$FileName, $str);
		$fileExt = $str->substring(($str->lastIndexOf('.') + 1));
		$this->SetValByKey(FrmAttachmentDBAttr::$FileExts, $fileExt);
	}
	public function getFileExts () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$FileExts);
	}
	public function setFileExts ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$FileExts, $value->replace(".", ""));
	}
	public function getFK_FrmAttachment () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$FK_FrmAttachment);
	}
	public function setFK_FrmAttachment ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$FK_FrmAttachment, $value);
		$idx = $value->lastIndexOf('_');
		$val = $value->substring(($idx + 1));
		$this->SetValByKey(FrmAttachmentDBAttr::$NoOfObj, $val);
	}
	public function getRefPKVal () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$RefPKVal);
	}
	public function setRefPKVal ($v) // [String v]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$RefPKVal, $v);
	}
	public function getFID () 
	{
		return $this->GetValInt64ByKey(FrmAttachmentDBAttr::$FID);
	}
	public function setFID ($value) // [long value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$FID, $value);
	}
	public function getMyNote () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$MyNote);
	}
	public function setMyNote ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$MyNote, $value);
	}
	public function getRec () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$Rec);
	}
	public function setRec ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$Rec, $value);
	}
	public function getRecName () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$RecName);
	}
	public function setRecName ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$RecName, $value);
	}
	public function getFK_MapData () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$FK_MapData);
	}
	public function setFK_MapData ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$FK_MapData, $value);
	}
	public function getFileSize () 
	{
		return $this->GetValFloatByKey(FrmAttachmentDBAttr::$FileSize);
	}
	public function setFileSize ($value) // [float value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$FileSize, ($value / 1024));
	}
	public function getIsRowLock () 
	{
		return $this->GetValBooleanByKey(FrmAttachmentDBAttr::$IsRowLock);
	}
	public function setIsRowLock ($value) // [boolean value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$IsRowLock, $value);
	}
	public function getIdx () 
	{
		return $this->GetValIntByKey(FrmAttachmentDBAttr::$Idx);
	}
	public function setIdx ($value) // [int value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$Idx, $value);
	}
	public function getNodeID () 
	{
		return $this->GetValStringByKey(FrmAttachmentDBAttr::$NodeID);
	}
	public function setNodeID ($value) // [String value]
	{
		$this->SetValByKey(FrmAttachmentDBAttr::$NodeID, $value);
	}
	public function getHisAttachmentUploadType () 
	{
		if (($this->getMyPK()->contains("_") && ($this->getMyPK()->length() < 32)))
		{
			return AttachmentUploadType::$Single;
		}
		else
		{
			return AttachmentUploadType::$Multi;
		}
	}
	public static function constructor__ () 
	{
		$me = new self();
		return $me;
	}
	public static function constructor__String ($mypk) // [String mypk]
	{
		$me = new self();
		$me->setMyPK($mypk);
		$me->Retrieve();
		return $me;
	}
	public function getEnMap () 
	{
		if (($this->get_enMap() != NULL))
		{
			return $this->get_enMap();
		}
		$map = new Map("Sys_FrmAttachmentDB", "附件数据存储");
		$map->Java_SetDepositaryOfEntity($Depositary->None);
		$map->Java_SetDepositaryOfMap($Depositary->Application);
		$map->Java_SetEnType($EnType->Sys);
		$map->IndexField = FrmAttachmentDBAttr::$RefPKVal;
		$map->AddMyPK();
		$map->AddTBString(FrmAttachmentDBAttr::$FK_MapData, NULL, "FK_MapData",  TRUE ,  FALSE , 1, 100, 20);
		$map->AddTBString(FrmAttachmentDBAttr::$FK_FrmAttachment, NULL, "附件主键",  TRUE ,  FALSE , 1, 500, 20);
		$map->AddTBString(FrmAttachmentDBAttr::$NoOfObj, NULL, "附件标识",  TRUE ,  FALSE , 0, 50, 20);
		$map->AddTBString(FrmAttachmentDBAttr::$RefPKVal, NULL, "实体主键",  TRUE ,  FALSE , 0, 50, 20);
		$map->AddTBInt(FrmAttachmentDBAttr::$FID, 0, "FID",  TRUE ,  FALSE );
		$map->AddTBString(FrmAttachmentDBAttr::$NodeID, NULL, "节点ID",  TRUE ,  FALSE , 0, 50, 20);
		$map->AddTBString(FrmAttachmentDBAttr::$Sort, NULL, "类别",  TRUE ,  FALSE , 0, 200, 20);
		$map->AddTBString(FrmAttachmentDBAttr::$FileFullName, NULL, "文件路径",  TRUE ,  FALSE , 0, 700, 20);
		$map->AddTBString(FrmAttachmentDBAttr::$FileName, NULL, "名称",  TRUE ,  FALSE , 0, 500, 20);
		$map->AddTBString(FrmAttachmentDBAttr::$FileExts, NULL, "扩展",  TRUE ,  FALSE , 0, 50, 20);
		$map->AddTBFloat(FrmAttachmentDBAttr::$FileSize, 0, "文件大小",  TRUE ,  FALSE );
		$map->AddTBDateTime(FrmAttachmentDBAttr::$RDT, NULL, "记录日期",  TRUE ,  FALSE );
		$map->AddTBString(FrmAttachmentDBAttr::$Rec, NULL, "记录�?",  TRUE ,  FALSE , 0, 50, 20);
		$map->AddTBString(FrmAttachmentDBAttr::$RecName, NULL, "记录人名�?",  TRUE ,  FALSE , 0, 50, 20);
		$map->AddTBStringDoc(FrmAttachmentDBAttr::$MyNote, NULL, "备注",  TRUE ,  FALSE );
		$map->AddTBInt(FrmAttachmentDBAttr::$IsRowLock, 0, "是否锁定�?",  TRUE ,  FALSE );
		$map->AddTBInt(FrmAttachmentDBAttr::$Idx, 0, "排序",  TRUE ,  FALSE );
		$map->AddTBString(FrmAttachmentDBAttr::$UploadGUID, NULL, "上传GUID",  TRUE ,  FALSE , 0, 500, 20);
		$map->AddTBAtParas(3000);
		$this->set_enMap($map);
		return $this->get_enMap();
	}
	protected function MakeFullFileFromFtp () 
	{
		$pathOfTemp = SystemConfig::getPathOfTemp();
		if (((new File($pathOfTemp))->isDirectory() ==  FALSE ))
		{
			(new File($pathOfTemp))->mkdirs();
		}
		$tempFile = (SystemConfig::getPathOfTemp() . $this->getFileName());
		if (((new File($tempFile))->isFile() ==  TRUE ))
		{
			(new File($tempFile))->delete();
		}
		return $tempFile;
	}
	protected function beforeInsert () 
	{
		return parent->beforeInsert();
	}
	protected function afterDelete () 
	{
		if ($DataType->IsNullOrEmpty($this->getFK_FrmAttachment()))
		{
			return ;
		}
		$ath = FrmAttachment::constructor__String($this->getFK_FrmAttachment());
		try 
		{
			if (($ath->getAthSaveWay() == AthSaveWay::$WebServer))
			{
				(new File($this->getFileFullName()))->delete();
			}
			if (($ath->getAthSaveWay() == AthSaveWay::$FTPServer))
			{
				$ftpUtil = $BP->WF->Glo->getFtpUtil();
				$fullName = $this->getFileFullName();
				$ftpUtil->deleteFile($fullName);
			}
		}
		catch (\RuntimeException $ex)
		{
			$Log->DebugWriteError($ex->getMessage());
		}
		$fkefs = $ath->GetParaString("FK_ExcelFile", NULL);
		if (($DataType->IsNullOrEmpty($fkefs) ==  FALSE ))
		{
			$efarr = $fkefs->split(",");
			$ef = NULL;
			$ets = NULL;
			foreach ($efarr as $fk_ef) 			{
				$ef = ExcelFile::constructor__();
				$ef->setNo($fk_ef);
				if (($ef->RetrieveFromDBSources() > 0))
				{
					$ets = ExcelTables::constructor__String($fk_ef);
					foreach ($ets->ToJavaList() as $et) 					{
						if (DBAccess->IsExitsObject($et->getNo()))
						{
							DBAccess::RunSQL($String->format("DELETE FROM %1$s WHERE FK_FrmAttachmentDB = '%2$s'", $et->getNo(), $this->getMyPK()));
						}
					}
				}
			}
		}
		parent->afterDelete();
	}
	public function GenerTempFile () 
	{
			/* match: AthSaveWay */
		return $this->GenerTempFile_AthSaveWay(AthSaveWay::$WebServer);
	}
	public function GenerTempFile_AthSaveWay ($saveWay) // [AthSaveWay saveWay]
	{
		if (($saveWay == AthSaveWay::$WebServer))
		{
			return $this->getFileFullName();
		}
		if (($saveWay == AthSaveWay::$FTPServer))
		{
			return $this->MakeFullFileFromFtp();
		}
		if (($saveWay == AthSaveWay::$DB))
		{
			throw new \RuntimeException("@尚未处理存储到db里面的文�?.");
		}
		throw new \RuntimeException("@尚未处理存储到db里面的文�?.");
	}
}
?>
